package com.skyline.jfs.storage.bean.impl;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.skyline.jfs.namespace.model.JFSDirectory;
import com.skyline.jfs.storage.bean.Storer;
import com.skyline.jfs.storage.bean.StorerProxy;
import com.skyline.jfs.storage.model.JFSStreamTranBlock;
import com.skyline.jfs.storage.service.Commander;

public class StorerProxyImpl implements StorerProxy {

	private static Map<String, Storer> storers = new HashMap<String, Storer>();

	private static Commander commander;
	
	private String ip;
	
	private String port;

	public StorerProxyImpl(String ip,String port) throws MalformedURLException, RemoteException, NotBoundException {
		super();
		this.ip=ip;
		this.port=port;
		commander = (Commander) Naming.lookup("rmi://"+ip+":"+port+"/commander");
		System.out.println("======StorerProxy has been created!======");
	}

	@Override
	public boolean store(String filePath, int replicNum, String fileName) throws RemoteException, NotBoundException,
			IOException {
		File file = new File(filePath);
		return store(file, replicNum, fileName);
	}

	@Override
	public boolean store(File file, int replicNum, String fileName) throws RemoteException, NotBoundException, IOException {
		if (file.exists() == false)
		{
			//TODO文件不存在，无法进行传输；
			return false;
		}
		List<String> storeNodeIp = commander.getJFSNodeToStore(replicNum);
		Long id=commander.createFile(fileName, file.length(), replicNum);
		if(id==-1){
			return false;
		}
		int nodeCount = 0;
		for (nodeCount = 0; nodeCount < storeNodeIp.size(); nodeCount++) {
			//for(int i=0;i<100;i++){
				System.out.println("server"+storeNodeIp.get(nodeCount));
			//}
			String ip = storeNodeIp.get(nodeCount);
			try {
				Storer storer = storers.get(ip);
				if (storer == null) {
					storer = (Storer) Naming.lookup("rmi://" + ip + ":"+port+"/storer");
					storers.put(ip, storer);
				}
				System.out.println("	connected!");
				InputStream in = null;
				byte[] buffer = new byte[JFSStreamTranBlock.BLOCK_SIZE];
				String key = String.valueOf((new Date()).getTime());
				in = new BufferedInputStream(new FileInputStream(file), JFSStreamTranBlock.BLOCK_SIZE);
				storer.startStore(key);
				while (in.read(buffer) > 0) {
					JFSStreamTranBlock block = new JFSStreamTranBlock();
					block.setContent(buffer);
					storer.store(key, block);
					System.out.println("		transmitting!");
				}
				storer.stopStore(key,id);
				System.out.println("	transmit finished!");
				System.out.println("Store " + file.getName() + " to " + ip );
				in.close();
			} catch (Exception e) {
				if (storers.get(ip) != null) {
					storers.remove(ip);
				}
				commander.invalidJFSNode(ip);
			}
		}
		if (nodeCount == storeNodeIp.size())
			return true;
		else
			return false;
	}
}
